<script>
	import {
		getToken
	} from '@/utils/auth.js';
	import {
		Update
	} from "@/api/user.js"
	export default {
		onLaunch: function() {
			console.log('App Launch')
			plus.navigator.closeSplashscreen();
			// console.log('App Launch');
			// app启动时打开启动广告页
			var w = plus.webview.open('hybrid/html/advertise/advertise.html', '本地地址', {
				top: 0,
				bottom: 0,
				zindex: 999
			}, 'fade-in', 5);

			//设置定时器，4s后关闭启动广告页
			setTimeout(function() {
				plus.webview.close(w);
			}, 15000);
			let haveToken = getToken()
			// #ifdef APP-PLUS
			// token标志来判断
			if (haveToken) {
				//存在则关闭启动页进入首页
				plus.navigator.closeSplashscreen();

			} else {
				//不存在则跳转至登录页
				uni.reLaunch({
					url: "/pages/login/index",
					success: () => {
						plus.navigator.closeSplashscreen();
					}
				})
			}
			// #endif

			// this.plusReady()
		},
		methods: {
			// 获取当前版本号
			plusReady() {
				var that = this;
				// 获取本地应用资源版本号  
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					// console.log(inf, 111111111)
					that.AndroidCheckUpdate(inf.version)
					uni.setStorageSync('version', inf.version)
				});

			},
			// 自动更新
			AndroidCheckUpdate(ver) {
				var that = this;
				Update(ver).then(res => {
					// console.log(1232, res);
					if (res.version > ver) {
						uni.setStorageSync('isNew', true)
						uni.showModal({
							title: '',
							content: '检测到有新版本可升级，是否更新',
							confirmText: '更新',
							confirmColor: '#EE8F57',
							success: function(res2) {
								if (res2.confirm) {
									uni.setStorageSync('version', res.version)
									if (uni.getSystemInfoSync().platform == 'android') {
										that.doUpData(res.androidUrl)
									} else if (uni.getSystemInfoSync().platform == 'ios') {
										that.doUpData(res.iosUrl)
									}
								}
							}
						});
					} else {
						uni.setStorageSync('isNew', false)
						// console.log("已是最新版本");
					}
				})
			},
			doUpData(urls) {
				uni.showLoading({
					title: '正在更新中……'
				})
				uni.downloadFile({ //执行下载
					url: urls,
					success: downloadResult => { //下载成功
						if (downloadResult.statusCode === 200) {
							uni.showModal({
								title: '',
								content: '更新成功，重启使用新版本',
								confirmText: '重启',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (res.confirm) {
										plus.runtime.install( //安装
											downloadResult.tempFilePath, {
												force: true
											},
											function() {
												utils.showToast('更新成功，重启中');
												plus.runtime.restart();
											},
											function(e) {
												utils.showToast('更新失败');
											}
										);
									}
								}
							});
						} else {
							uni.showLoading({
								title: '更新失败，请联系管理员'
							})
						}
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			},
			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}
</style>
